import http from 'http'
import Koa from 'koa'
import { bodyParser } from '@koa/bodyparser'
import cors from '@koa/cors'
import { defaultStaticJsonFileConfigCenter as config } from './infra/config/bootstrap.mjs'
import { router } from './router/index.mjs'
import { deviceGateway } from './ws/device-gateway.mjs'
import { logger } from './infra/logger.mjs'

const app = new Koa()
app
  .use(cors())
  .use(bodyParser())
  .use(router.routes())
  .use(router.allowedMethods())

const server = http.createServer(app.callback())

deviceGateway.start(server)

const port = config.get('server.port')
const host = config.get('server.host')

server.listen(port, host, () => {
  logger.info('SmartPot message bus started', { port, host })
})